package com.enba.rbacadmin.module.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.enba.boot.core.base.Result;
import com.enba.boot.core.utils.TreeNode;
import com.enba.rbacadmin.annotation.DistributedLock;
import com.enba.rbacadmin.annotation.NoNeedValidation;
import com.enba.rbacadmin.module.auth.controller.req.OrgAddUpdateReq;
import com.enba.rbacadmin.module.auth.entity.Org;
import com.enba.rbacadmin.module.auth.service.IOrgService;
import com.enba.rbacadmin.utils.CodeGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户组织表
 *
 * @author 恩爸编程
 * @since 2025-02-28
 */
@RestController
@RequestMapping("/auth/org")
@Api(tags = "用户组织表接口")
public class OrgController {

  @Resource private IOrgService orgService;
  @Resource private CodeGenerator codeGenerator;

  /**
   * 查询所有接口
   *
   * @return list
   */
  @GetMapping
  public Result<List<Org>> findAll() {
    return Result.success(orgService.list());
  }

  /**
   * 根据id查询数据接口
   *
   * @param id id
   * @return result
   */
  @GetMapping("/{id}")
  public Result<Org> findOne(@PathVariable Integer id) {
    return Result.success(orgService.getById(id));
  }

  /**
   * 分页查询接口
   *
   * @param pageNum nume
   * @param pageSize size
   * @return result
   */
  @GetMapping("/page")
  public Result<Page<Org>> findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
    QueryWrapper<Org> queryWrapper = new QueryWrapper<>();
    return Result.success(orgService.page(new Page<>(pageNum, pageSize), queryWrapper));
  }

  /** 新增和更新接口 */
  @DistributedLock
  @PostMapping
  public Result<Boolean> saveOrUpdate(@RequestBody @Validated OrgAddUpdateReq req) {
    return Result.success(orgService.saveOrUpdate(req));
  }

  /**
   * 删除接口
   *
   * @param id id
   * @return result
   */
  @DeleteMapping("/{id}")
  public Result<Boolean> delete(@PathVariable Integer id) {
    return Result.success(orgService.removeById(id));
  }

  /**
   * 批量删除接口
   *
   * @param ids ids
   * @return result
   */
  @PostMapping("/del/batch")
  public Result<Boolean> deleteBatch(@RequestBody List<Integer> ids) {
    return Result.success(orgService.removeByIds(ids));
  }

  @ApiOperation("获取所有组织，树形结构返回")
  @GetMapping("/all-tree-org")
  public Result<List<TreeNode>> getAllTreeOrg() {
    return Result.success(orgService.getAllOrg());
  }

  @ApiOperation("获取所有组织，树形表格结构返回")
  @GetMapping("/all-treeTable-org")
  public Result<List<TreeNode>> getAllTreeTableOrg() {
    return Result.success(orgService.getAllTreeTableOrg());
  }

  // 获取组织机构编码
  @NoNeedValidation
  @ApiOperation("获取组织机构编码测试")
  @GetMapping("/orgGenerateCode")
  public Result<String> getOrgCode() {
    return Result.success(codeGenerator.orgGenerateCode());
  }
}
